package com.wyh.service.serviceImpl.g3serviceimpl;
import com.wyh.domain.po.g3po.*;
import com.wyh.mapper.g3mapper.*;
import com.wyh.service.g3service.GenerateBillingContent;
import com.wyh.strategy.g3impl.CalculationFee;
import com.wyh.utils.g3utils.BillidGenerateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/05/28/16:26
 **/
@Service
public class GenerateBillingContentImpl implements GenerateBillingContent {
    @Autowired
    G3ContractMapper g3ContractMapper;
    @Autowired
    ContractItemMapper contractItemMapper;
    @Autowired
    G3BillingItemsMapper g3BillingItemsMapper;
    @Autowired
    G3ResourceMapper g3ResourceMapper;
    @Autowired
    ThreeMeterMapper threeMeterMapper;

    @Autowired
    G3UserMapper g3UserMapper;
    @Autowired
    G3CustomerMapper g3CustomerMapper;
    @Autowired
    CalculationFee calculationFee;

    @Override
    public List<Charging> GenerateBilling(G3Contract g3Contract) {
        List<Charging> chargingList=new ArrayList<>();
        //根据合同id查询该合同下计费项
        List<G3Contractitem> g3ContractitemList =contractItemMapper.getAllContractItemsByContract(g3Contract);
        if(g3ContractitemList.size()>0) {
            //检索每一个计费项的信息
            for (G3Contractitem c : g3ContractitemList) {
                Charging charging = new Charging();
                //生成账单id
                charging.setBillid(Long.parseLong(BillidGenerateUtils.generateFixedLengthIndex()));
                //生成计费资源名称
                charging.setResourcename(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getName());
                //生成计费资源id
                charging.setResourceid(Integer.parseInt(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getNumber()));
                //生成计费项目
                /*2024.6.4需要将剩余数据封装到charging*/
                charging.setBillingitemsname(c.getName());
                //生成计费项目id
                charging.setUnitpriceid(g3BillingItemsMapper.getBillingItemsByContract(c).getId());
                //生成计费性质
                //String type= BillingItemsEnum.BillingType.getByCode(billingItemsMapper.getBillingItemsByContract(c).getType());
                //billDetails1.setBillingitems(type);
                charging.setBillingnature(g3BillingItemsMapper.getBillingItemsByContract(c).getType());
                //生成计费性质id  /****多余****/
                //billDetails1.setBillingnature(billingItemsMapper.getBillingItemsByContract(c).getType()+"");

                //生成建筑面积
                charging.setArea(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getBuildingarea());
                //生成单价
                charging.setPrice(c.getPrice());
                //生成客户id
                charging.setCustomerid(g3Contract.getCustomerid());
                //生成计费时间
                // 获取当前的LocalDateTime对象，表示当前时间
                LocalDateTime now = LocalDateTime.now();
                // 将LocalDateTime转换为Timestamp
                Timestamp currentTimestamp = Timestamp.valueOf(now);
                charging.setBillingtime(currentTimestamp);
                //生成客户名称 customername
                charging.setCustomername(g3CustomerMapper.getCostomerByContract(g3Contract).getName());
                //***生成修改时间updatetime
                if(threeMeterMapper.getThreeMeterReadingByContractitem(c)!=null) {
                    //生成计费开始时间
                    charging.setStarttime(threeMeterMapper.getThreeMeterReadingByContractitem(c).getPreviousperiodtime());
                    //生成用量
                    charging.setDosage(threeMeterMapper.getThreeMeterReadingByContractitem(c).getDosage());
                    //计费结束时间
                    charging.setEndtime(threeMeterMapper.getThreeMeterReadingByContractitem(c).getReadingtime());
                }
                //合同id contractid
                charging.setContractid(g3Contract.getId());
//                charging.setReceivable(BigDecimal.valueOf(calculationFee.calculationBillAmount(c)));
                //四舍五入到分
                charging.setReceivable(BigDecimal.valueOf(calculationFee.calculationBillAmount(c))
                        .setScale(2, RoundingMode.HALF_UP));

                //楼盘名称 propertyname
                BuildInfo buildInfo = new BuildInfo();
                ResourceInfo resourceInfo = new ResourceInfo();
                resourceInfo = g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract);
                if(resourceInfo!=null) {
                    buildInfo.setPropertyname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getPropertyname());
                    buildInfo.setBuildingname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getBuildingname());
                    buildInfo.setFloorname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getFloorname());
                }
                //设置楼盘名称
                charging.setPropertyname(buildInfo.getPropertyname());
                //楼栋名称 buildingname
                charging.setBuildingname(buildInfo.getBuildingname());
                //楼层名称 floorname
                charging.setFloorname(buildInfo.getFloorname());
                //***状态 state  待入账
                charging.setState(0);
                //***备注 remarks
                chargingList.add(charging);

            }
        }
        return chargingList;
    }

    @Override
    public List<Charging> updateBilling(Integer customerid, int days) {
        List<Charging>chargingList=new ArrayList<>();
        List<G3Contract> g3ContractList = g3ContractMapper.getContractByCustomerId(customerid);
        if(g3ContractList.size()>0){
            for (G3Contract g3Contract : g3ContractList) {
                List<G3Contractitem> g3ContractitemList =contractItemMapper.getAllContractItemsByContract(g3Contract);
                if(g3ContractitemList.size()>0){
                    Charging charging=new Charging();
                    for (G3Contractitem g3Contractitem : g3ContractitemList) {
                        //周期性计费类型
                        if(g3BillingItemsMapper.getBillType(g3Contractitem)==0){
                            //获取合同周期
                            int contractDays= g3Contract.getBillingcycle();
                            float proportion=(float)days/contractDays;
                            double money=calculationFee.calculationBillAmount(g3Contractitem)*proportion;
                            charging.setReceivable(BigDecimal.valueOf(money));
                        }
                        //三表类计费类型
                        if(g3BillingItemsMapper.getBillType(g3Contractitem)==1){
                            charging.setReceivable(BigDecimal.valueOf(calculationFee.calculationBillAmount(g3Contractitem)));
                            }
                        //生成账单id
                        charging.setBillid(Long.parseLong(BillidGenerateUtils.generateFixedLengthIndex()));
                        //生成计费资源名称
                        charging.setResourcename(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getName());
                        //生成计费资源id
                        charging.setResourceid(Integer.parseInt(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getNumber()));
                        //生成计费项目
                        /*2024.6.4需要将剩余数据封装到charging*/
                        charging.setBillingitemsname(g3BillingItemsMapper.getBillingItemsByContract(g3Contractitem).getName());
                        //生成计费项目id
                        charging.setUnitpriceid(g3BillingItemsMapper.getBillingItemsByContract(g3Contractitem).getId());
                        //生成计费性质
                        //String type= BillingItemsEnum.BillingType.getByCode(billingItemsMapper.getBillingItemsByContract(c).getType());
                        //billDetails1.setBillingitems(type);
                        charging.setBillingnature(g3BillingItemsMapper.getBillingItemsByContract(g3Contractitem).getType());
                        //生成计费性质id  /****多余****/
                        //billDetails1.setBillingnature(billingItemsMapper.getBillingItemsByContract(c).getType()+"");

                        //生成建筑面积
                        charging.setArea(g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract).getBuildingarea());
                        //生成单价
                        charging.setPrice(g3Contractitem.getPrice());
                        //生成客户id
                        charging.setCustomerid(g3Contract.getCustomerid());
                        //生成计费时间
                        // 获取当前的LocalDateTime对象，表示当前时间
                        LocalDateTime now = LocalDateTime.now();
                        // 将LocalDateTime转换为Timestamp
                        Timestamp currentTimestamp = Timestamp.valueOf(now);
                        charging.setBillingtime(currentTimestamp);
                        //生成客户名称 customername
                        charging.setCustomername(g3CustomerMapper.getCostomerByContract(g3Contract).getName());
                        //***生成修改时间updatetime
                        if(threeMeterMapper.getThreeMeterReadingByContractitem(g3Contractitem)!=null) {
                            //生成计费开始时间
                            charging.setStarttime(threeMeterMapper.getThreeMeterReadingByContractitem(g3Contractitem).getPreviousperiodtime());
                            //生成用量
                            charging.setDosage(threeMeterMapper.getThreeMeterReadingByContractitem(g3Contractitem).getDosage());
                            //计费结束时间
                            charging.setEndtime(threeMeterMapper.getThreeMeterReadingByContractitem(g3Contractitem).getReadingtime());
                        }
                        //合同id contractid
                        charging.setContractid(g3Contract.getId());

                        //楼盘名称 propertyname
                        BuildInfo buildInfo = new BuildInfo();
                        ResourceInfo resourceInfo = new ResourceInfo();
                        resourceInfo = g3ResourceMapper.getResourceInfoByContractResourceId(g3Contract);
                        if(resourceInfo!=null) {
                            buildInfo.setPropertyname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getPropertyname());
                            buildInfo.setBuildingname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getBuildingname());
                            buildInfo.setFloorname(g3ResourceMapper.getBuildInfoByResourceId(resourceInfo).getFloorname());
                        }
                        //设置楼盘名称
                        charging.setPropertyname(buildInfo.getPropertyname());
                        //楼栋名称 buildingname
                        charging.setBuildingname(buildInfo.getBuildingname());
                        //楼层名称 floorname
                        charging.setFloorname(buildInfo.getFloorname());
                        //***状态 state  待入账
                        charging.setState(0);
                        //***备注 remarks
                        chargingList.add(charging);
                    }






                }
            }
        }
        return chargingList;
    }
}
